package cn.elwin.samples;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class FormWidgets extends Activity {

	private RadioGroup radio_group;
	private RadioButton radio_button_left;
	private RadioButton radio_button_right;

	private CheckBox check_box_one;
	private CheckBox check_box_two;
	private CheckBox check_box_three;

	private ProgressBar progress_bar_defaule;
	private ProgressBar progress_bar_horizontal;
	private Button progress_bar_button;
	private int progress_bar_rate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_form_widgets);

		/* RadioGroup */
		radio_group = (RadioGroup) findViewById(R.id.radio_group);
		radio_button_left = (RadioButton) findViewById(R.id.radio_button_left);
		radio_button_right = (RadioButton) findViewById(R.id.radio_button_right);

		radio_button_left.setChecked(true);

		radio_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if (radio_button_left.getId() == checkedId) {
					System.out.println("checked left(male)");
					Toast.makeText(FormWidgets.this, "checked left(male)",
							Toast.LENGTH_SHORT).show();
				} else if (radio_button_right.getId() == checkedId) {
					System.out.println("checked left(female)");
					Toast.makeText(FormWidgets.this, "checked left(female)",
							Toast.LENGTH_SHORT).show();
				} else {
					System.out.println("unknow id of radio_group");
				}
			}
		});

		/* CheckBox */
		check_box_one = (CheckBox) findViewById(R.id.check_box_one);
		check_box_two = (CheckBox) findViewById(R.id.check_box_two);
		check_box_three = (CheckBox) findViewById(R.id.check_box_three);

		check_box_one
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							System.out
									.println("checked check box one(reading)");
							Toast.makeText(FormWidgets.this,
									"checked check box one(reading)",
									Toast.LENGTH_SHORT).show();
						} else {
							System.out
									.println("unchecked check box one(reading)");
							Toast.makeText(FormWidgets.this,
									"unchecked check box one(reading)",
									Toast.LENGTH_SHORT).show();
						}
					}
				});

		check_box_two
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							System.out
									.println("checked check box two(singing)");
							Toast.makeText(FormWidgets.this,
									"checked check box two(singing)",
									Toast.LENGTH_SHORT).show();
						} else {
							System.out
									.println("unchecked check box two(singing)");
							Toast.makeText(FormWidgets.this,
									"unchecked check box two(singing)",
									Toast.LENGTH_SHORT).show();
						}
					}
				});

		check_box_three
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							System.out
									.println("checked check box three(swimming)");
							Toast.makeText(FormWidgets.this,
									"checked check box three(swimming)",
									Toast.LENGTH_SHORT).show();
						} else {
							System.out
									.println("unchecked check box three(swimming)");
							Toast.makeText(FormWidgets.this,
									"unchecked check box three(swimming)",
									Toast.LENGTH_SHORT).show();
						}
					}
				});

		/* ProgressBar */
		progress_bar_button = (Button) findViewById(R.id.progress_bar_button);
		progress_bar_rate = -1;
		progress_bar_button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				progress_bar_defaule = (ProgressBar) findViewById(R.id.progress_bar_default);
				progress_bar_horizontal = (ProgressBar) findViewById(R.id.progress_bar_horizontal);

				if (progress_bar_rate < 0) {
					progress_bar_defaule.setVisibility(View.VISIBLE);
					progress_bar_horizontal.setVisibility(View.VISIBLE);
					progress_bar_rate = 0;
				} else if (progress_bar_rate < 100) {
					progress_bar_horizontal.setProgress(progress_bar_rate);
					progress_bar_horizontal
							.setSecondaryProgress(progress_bar_rate + 10);
					progress_bar_defaule.setProgress(progress_bar_rate);
					progress_bar_rate += 10;
				} else {
					progress_bar_defaule.setVisibility(View.GONE);
					progress_bar_horizontal.setVisibility(View.GONE);
					progress_bar_horizontal.setProgress(0);
					progress_bar_horizontal.setSecondaryProgress(0);
					progress_bar_defaule.setProgress(0);
					progress_bar_rate = -1;
					return;
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.form_widgets, menu);
		return true;
	}

}
